<?php
namespace Shopmanage\Model;
use Think\Model\ViewModel;
class EventUserGiftViewModel extends ViewModel {
	public $sid;
	public $eid;
	public $uid;
	public $wharr;
   public $viewFields = array(
	 'user_gift'=>array('_table'=>'sm_users_gifts','id'=>'user_gift_id','sid','uzuid','actiontime','giftid','eventid','tradeid','status','gtype','quantity','kamiid','wlid','wlcode','getway','event_user_id','is_picup','picup_time','gift_num','user_nick','gift_name','gift_pic_url','extendvar1','extendvar2','extendvar3','extendint1','extendint2','extendint3','sys_giftid','_type'=>'LEFT'),
	 'gifts'=>array('_table'=>'sm_events_gifts', 'type','level','status','name','details','price','probability','isshow','quantity','quantity_initial','zailai','jifen','medal_id','medal_name','medal_pic','pic_url','link','num_iids','event_id','promoid','tagid','_on'=>'gifts.id=user_gift.giftid','_type'=>'LEFT'),
	 'user_address'=>array('_table'=>'sm_user_address', 'kxdr_uid','shop_uid','sys_uid','nationality','province','city','city_part','address','name','tel','is_def','_on'=>'user_address.shop_uid=user_gift.uzuid','_type'=>'LEFT'),
	 'sendlog'=>array('_table'=>'sm_gifts_sendlog','id'=>'send_id','send_time'=>"item_send_time", 'is_auto_send','sender_nick','nationality','province','city','city_part','address','true_name','mobile','wlname','wlid','wlcode','version','_on'=>'sendlog.ugid=user_gift.id','_type'=>'LEFT'),
	 'shop_user'=>array('_table'=>'sm_shop_users','sid','addtime','firstevent','lastlogin','lastseeds','lotts','wins','seeds','nextseeds','rate_good','rate_neutral','rate_bad','his_trades','trads','his_pay','sumpay','_on'=>'shop_user.id = user_gift.uzuid','_type'=>'LEFT'),
	 'user'=>array('_table'=>'sm_user','mixednick','_on'=>'user.id=shop_user.userid','_type'=>'LEFT'),
	 );
	 
	 public function sendaddress_download(){
		unset($this->viewFields["sendlog"]);
		unset($this->viewFields["user"]);
		unset($this->viewFields["shop_user"]);
		unset($this->viewFields["gifts"]);
		$whe["sid"]=$this->sid;
		$whe["wlid"]=array('exp',' IS NULL ');
		$whe["gtype"]=1; //只有实物奖需要地址信息
		$data = $this->where($whe)->getField("user_gift_id,name,tel,province,city,city_part,address,gift_name,gift_num,actiontime");
		$str = "业务单号,收件人姓名,收件人手机,收件省,收件市,收件区/县,收件人地址,奖品名,数量,中奖时间\r\n";   
		foreach($data as $k=>$gift){
			$str.=$data[$k]["user_gift_id"].","
				 .$data[$k]["name"].","
				 .$data[$k]["tel"].","
				 .$data[$k]["province"].","
				 .$data[$k]["city"].","
				 .$data[$k]["city_part"].","
				 .$data[$k]["address"].","
				 .$data[$k]["gift_name"].","
				 .$data[$k]["gift_num"].","
				 .date("Y-m-d H:i:s",strtotime($data[$k]["actiontime"])).","
				 ."\r\n";
		}
		
		$filename = "中奖信息".date('Ymd').'.csv'; //设置文件名   
		// header("Content-type: application/vnd.ms-excel; charset=gb2312" );
		header("Content-type:text/csv");   
		header("Content-Disposition:attachment;filename=".$filename);   
		header('Cache-Control:must-revalidate,post-check=0,pre-check=0');   
		header('Expires:0');   
		header('Pragma:public');
		
		echo mb_convert_encoding($str,"gb2312","utf-8");  
	 }
	public function gift_list_download(){
		unset($this->viewFields["user_address"]);
		$whe["sid"]=$this->sid;
		$data = $this->where($whe)->getField("user_gift_id,type,level,gift_num,price,gift_name,actiontime,kamiid,wlid,wlcode,getway,item_send_time,sender_nick,province,city,city_part,address,true_name,mobile,sendlog.wlname,sendlog.wlid,sendlog.wlcode,mixednick");
		$str = "奖品类型,奖品等级,中奖数量,奖品价值,奖品名称,中奖人,中奖方式,中奖时间,寄出时间,邮寄地址,收件人,电话,单号,物流,操作人,\r\n";   
		$tools = C("GFT_TYPE");
		$getway = C("KXDR_GETWAY");
		$wl = C("WL");
		foreach($data as $k=>$gift){
			$data[$k]["level"]=getlevel($data[$k]["level"]);
			$data[$k]["getway"] = $getway[$data[$k]["getway"]]["name"];
			foreach($tools as $tool){
				if($data[$k]["type"]==$tool["id"]){
					$data[$k]["type"]=strip_tags($tool["name"]);
				}
			}
			$str.=$data[$k]["type"].","
				 .strip_tags($data[$k]["level"]).","
				 .$data[$k]["gift_num"]."份,"
				 .$data[$k]["price"]."元,"
				 .$data[$k]["gift_name"].","
				 .$data[$k]["mixednick"].","
				 .$data[$k]["getway"].","
				 .$data[$k]["actiontime"].","
				 .$data[$k]["item_send_time"].","
				 .$data[$k]["province"].$data[$k]["city"].$data[$k]["city_part"].$data[$k]["address"].","
				 .$data[$k]["true_name"].","
				 .$data[$k]["mobile"].","
				 .$data[$k]["wlid"].","
				 .$wl[$data[$k]["wlcode"]].","
				 .$data[$k]["sender_nick"].","
				 ."\r\n";
		}
		$filename = "中奖信息".date('Ymd').'.csv'; //设置文件名   
		// header("Content-type: application/vnd.ms-excel; charset=gb2312" );
		header("Content-type:text/csv");   
		header("Content-Disposition:attachment;filename=".$filename);   
		header('Cache-Control:must-revalidate,post-check=0,pre-check=0');   
		header('Expires:0');   
		header('Pragma:public');
		
		echo mb_convert_encoding($str,"gb2312","utf-8");  
	
	}
}